package com.itmentu.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

/**
 * @Author：http://www.itmentu.com
 * @date： 2021/12/6 20:40
 * @Description：
 ***/
//@Configuration
public class PublisherDirectExchangeConfig {

    /**
     * 创建交换机
     */
    @Bean
    public DirectExchange directPublisherExchange(){
        return ExchangeBuilder
                //交换机名字
                .directExchange("direct_publisher_exchange")
                //数据持久化
                .durable(true)
                //构建交换机
                .build();
    }

    /**
     * 声明队列
     */
    @Bean
    public Queue directPublisherQueue(){
        return QueueBuilder
                //指定队列名字
                .durable("direct_publisher_queue")
                //构建队列
                .build();
    }

    /**
     * 队列绑定交换机
     */
    @Bean
    public Binding directPublisherBinding(@Qualifier("directPublisherQueue") Queue queue,
                                 @Qualifier("directPublisherExchange") Exchange exchange){
        return BindingBuilder
                //将队列queue
                .bind(queue)
                //绑定到exchange上
                .to(exchange)
                //direct交换机的路由规则不写
                .with("publisher")
                //路由没有别的参数
                .noargs();
    }

    @Bean
    public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        // 设置开启Mandatory，才能触发回调函数，无论消息推送结果怎么样都强制调用回调函数
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack,String cause) {
                System.out.println("ConfirmCallback    ：相关数据：" + correlationData);
                System.out.println("ConfirmCallback    ：投递服务是否成功：" + ack);
                System.out.println("ConfirmCallback    ：原因：" + cause);
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("ReturnCallback    ：消息：" + message);
                System.out.println("ReturnCallback    ：回应码：" + replyCode);
                System.out.println("ReturnCallback    ：回应消息：" + replyText);
                System.out.println("ReturnCallback    ：交换机：" + exchange);
                System.out.println("ReturnCallback    ：路由key：" + routingKey);
                System.out.println("ReturnCallback=========================================");
            }
        });
        return rabbitTemplate;
    }
}
